package com.mall.controller;

import com.github.pagehelper.PageInfo;
import com.mall.common.Result;
import com.mall.filter.UserFilter;
import com.mall.model.dto.CreateOrderRequest;
import com.mall.model.vo.OrderVo;
import com.mall.service.OrderService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


        @RestController
        public class OrderController {
            @Autowired
            private OrderService orderService;
            @ApiOperation("创建订单")
            @PostMapping("/order/create")
            public Result createOrder(@RequestBody @Valid CreateOrderRequest createOrderRequest){
                String orderNo = orderService.createOrder(UserFilter.currentUser.getId(),createOrderRequest);
        return Result.success(orderNo);
    }


    @ApiOperation("订单详情")
    @GetMapping("/order/detail")
    public Result detailOrder(@RequestParam("orderNo") String orderNo){
        OrderVo orderVo = orderService.detail(orderNo);
        return Result.success(orderVo);
    }

    @ApiOperation("前台订单列表")
    @GetMapping("/order/list")
    public Result listForCustomer(@RequestParam Integer pageNum,@RequestParam Integer pageSize){
    PageInfo pageInfo = orderService.listForCustomer(pageNum,pageSize,UserFilter.currentUser.getId());
    return Result.success(pageInfo);}
//            取消订单
    @ApiOperation("前台取消")
    @PostMapping("/order/cancel")
    public Result cancel(@RequestParam("orderNo") String orderNo){
//         通过订单号查找订单
    orderService.cancel(orderNo);
    return Result.success();
    }

    @ApiOperation("生成支付二维码")
    @GetMapping("/order/qrcode")
    public Result qrcode(@RequestParam("orderNo") String orderNo){
     String qrurl = orderService.qrcode(orderNo);
      return Result.success(qrurl);
  }
  @ApiOperation("支付操作")
  @GetMapping("/order/pay")
  public Result pay(@RequestParam("orderNo") String orderNo){
      orderService.pay(orderNo);
      return Result.success();
  }
//  后台订单列表
@GetMapping("/admin/order/list")
  public Result adminList(@RequestParam Integer pageNum,@RequestParam Integer pageSize){
     PageInfo pageInfo = orderService.listForAdmin(pageNum,pageSize);
     return Result.success(pageInfo);
  }

@PostMapping("/admin/order/delivered")
 public Result deliver(@RequestParam("orderNo") String orderNo){
  orderService.deliver(orderNo);
  return Result.success();
 }
@PostMapping("/order/finish")
 public Result finish(@RequestParam("orderNo") String orderNo){
  orderService.finish(orderNo);
    return Result.success();
            }
}
